package com.zxl.always.on.display.ui.widget.clock;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.zxl.always.on.display.R;
import com.zxl.always.on.display.model.PaletteSet;
import com.zxl.always.on.display.tools.BitmapUtils;
import com.zxl.always.on.display.ui.widget.NotificationLayout;
import com.zxl.always.on.display.ui.widget.battery.BatteryLayout;

/**
 * Created by Administrator on 2018/2/28.
 */

public abstract class BaseClockView extends FrameLayout implements StandAloneClock {

    protected int mDigitalStyle = 0;

    private ImageView mBackgroundView;

    protected FrameLayout mClockLayout;

    protected BatteryLayout mBatteryLayout;

    protected NotificationLayout mNotificationLayout;

    public BaseClockView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mBackgroundView = findViewById(R.id.background);
        mClockLayout = findViewById(R.id.clock_view);
        mBatteryLayout = findViewById(R.id.panel_battery_layout);
        mNotificationLayout = findViewById(R.id.notification_container);
        setDigitalStyle(mDigitalStyle);
    }

    @Override
    public void pauseStandAloneMode() {
        View view = mClockLayout.getChildAt(0);
        if (StandAloneClock.class.isInstance(view)) {
            StandAloneClock standAloneClock = (StandAloneClock) view;
            standAloneClock.pauseStandAloneMode();
        }
    }

    @Override
    public void refreshTime() {
        View view = mClockLayout.getChildAt(0);
        if (StandAloneClock.class.isInstance(view)) {
            StandAloneClock standAloneClock = (StandAloneClock) view;
            standAloneClock.refreshTime();
        }
    }

    @Override
    public void resumeStandAloneMode() {
        View view = mClockLayout.getChildAt(0);
        if (StandAloneClock.class.isInstance(view)) {
            StandAloneClock standAloneClock = (StandAloneClock) view;
            standAloneClock.resumeStandAloneMode();
        }
    }

    @Override
    public void setStandAloneMode(boolean isStandAlone) {
        View view = mClockLayout.getChildAt(0);
        if (StandAloneClock.class.isInstance(view)) {
            StandAloneClock standAloneClock = (StandAloneClock) view;
            standAloneClock.setStandAloneMode(isStandAlone);
        }
    }

    @Override
    public void setPaletteColor(int position) {
        View view = mClockLayout.getChildAt(0);
        if (StandAloneClock.class.isInstance(view)) {
            StandAloneClock standAloneClock = (StandAloneClock) view;
            standAloneClock.setPaletteColor(position);
        }
    }

    @Override
    public void setBackgroundStyle(int position) {
        int resourceArray = PaletteSet.BG_RESOURCE_ARRAYS[position];
        if (resourceArray == -1) {
            mBackgroundView.setImageDrawable(null);
        } else {
            mBackgroundView.setImageResource(resourceArray);
        }
    }

    public void startBatteryMonitor() {
        mBatteryLayout.setIsStartMonitor(true);
    }

    public void stopBatteryMonitor() {
        mBatteryLayout.setIsStartMonitor(false);
    }

    public void addNotification(Drawable drawable) {
        mNotificationLayout.addNotification(drawable);
    }

    public void setDigitalStyle(int mDigitalStyle) {
        this.mDigitalStyle = mDigitalStyle;
        refreshStyle();
    }

    public abstract void refreshStyle();

    public Bitmap getSettingPreview(int itemSize) {
        refreshTime();
        addNotification(getResources().getDrawable(R.drawable.missed_call));
        addNotification(getResources().getDrawable(R.drawable.missed_sms));

        setDrawingCacheEnabled(true);
        measure(View.MeasureSpec.makeMeasureSpec(getResources().getDisplayMetrics().widthPixels, View.MeasureSpec.EXACTLY),
                View.MeasureSpec.makeMeasureSpec(getResources().getDisplayMetrics().heightPixels, View.MeasureSpec.EXACTLY));
        layout(0, 0, getMeasuredWidth(), getMeasuredHeight());
        buildDrawingCache(true);
        setDrawingCacheEnabled(false);
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        Bitmap bitmap = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.translate(0, (width - height) / 2.0F);
        draw(canvas);
        destroyDrawingCache();
        bitmap = Bitmap.createScaledBitmap(bitmap, itemSize, itemSize, true);
        return BitmapUtils.getRoundedCornerBitmap(getContext(), bitmap, getContext().getResources().getDimensionPixelSize(R.dimen.settings_preview_rounded_level));
    }

    public Bitmap getCheckPreview(int itemSize) {
        refreshTime();
        setDrawingCacheEnabled(true);
        measure(View.MeasureSpec.makeMeasureSpec(getResources().getDisplayMetrics().widthPixels, View.MeasureSpec.EXACTLY),
                View.MeasureSpec.makeMeasureSpec(getResources().getDisplayMetrics().heightPixels, View.MeasureSpec.EXACTLY));
        layout(0, 0, getMeasuredWidth(), getMeasuredHeight());
        buildDrawingCache(true);
        setDrawingCacheEnabled(false);
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        Bitmap bitmap = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.translate(0, (width - height) / 2.0F);
        draw(canvas);
        destroyDrawingCache();
        bitmap = Bitmap.createScaledBitmap(bitmap, itemSize, itemSize, true);
        return BitmapUtils.getRoundedCornerBitmap(getContext(), bitmap, getContext().getResources().getDimensionPixelSize(R.dimen.settings_preview_rounded_level));
    }

    public int getDigitalStyle() {
        return mDigitalStyle;
    }
}
